package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.LandmarkContent;
import cn.wolfcode.trip.base.domain.LandmarkDetai;
import cn.wolfcode.trip.base.domain.LtCatalogDetail;
import cn.wolfcode.trip.base.mapper.LandmarkContentMapper;
import cn.wolfcode.trip.base.mapper.LandmarkDetaiMapper;
import cn.wolfcode.trip.base.mapper.LtCatalogDetailMapper;
import cn.wolfcode.trip.base.query.landmarkDetailQueryObject;
import cn.wolfcode.trip.base.service.ILandmarkDetaiService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LandmarkDetaiServiceImpl implements ILandmarkDetaiService {
    @Autowired
    private LandmarkDetaiMapper landmarkDetaiMapper;
    @Autowired
    private LandmarkContentMapper landmarkContentMapper;
    @Autowired
    private LtCatalogDetailMapper  ltCatalogDetailMapper;


    @Override
    public void save(LandmarkDetai landmarkDetai) {
        landmarkDetaiMapper.insert(landmarkDetai);
    }

    @Override
    public void delete(Long id) {
        landmarkDetaiMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<LandmarkDetai> selectAll() {
        return landmarkDetaiMapper.selectAll();
    }

    @Override
    public void update(LandmarkDetai landmarkDetai) {
        landmarkDetaiMapper.updateByPrimaryKey(landmarkDetai);
    }

    @Override
    public LandmarkDetai get(Long id) {
        return landmarkDetaiMapper.selectByPrimaryKey(id);
    }

    //大标题
    @Override
    public List<LandmarkDetai> getLmDetailByState(long state) {
        return landmarkDetaiMapper.selectLmDetailByState(state);
    }

    @Override
    public LandmarkContent getContentById(long id) {
        return landmarkContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo getDetailsByIcdId(landmarkDetailQueryObject qo) {
        //当前页,和页面显示条数
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //准备基本sql
        List<LandmarkDetai> list = landmarkDetaiMapper.selectDetailsByIcdId(qo.getIcdId(),qo.getDiscount());
        return new PageInfo(list);
    }

    @Override
    public LtCatalogDetail getCatalogBigId(Long cbId) {
        return ltCatalogDetailMapper.selectByPrimaryKey(cbId);
    }

    @Override
    public void saveOrUpdate(LandmarkDetai landmarkDetai) {
        //假如推荐头条消除之前的门票头条
        if(landmarkDetai.getDiscount()==LandmarkDetai.hasDiscount){
            List<LandmarkDetai> lans = landmarkDetaiMapper.selectLmDetailByState(1);
            LandmarkDetai lan=lans.get(0);
            lan.setDiscount(0L);
            landmarkDetaiMapper.updateByPrimaryKey(lan);
        }

        if(landmarkDetai.getId()!=null){
            landmarkDetaiMapper.updateByPrimaryKey(landmarkDetai);
            LandmarkContent con=new LandmarkContent();
            con.setContent(landmarkDetai.getLandmarkContent().getContent());
            con.setId(landmarkDetai.getId());
            landmarkContentMapper.updateByPrimaryKey(con);
        }else{
            landmarkDetaiMapper.insert(landmarkDetai);
            LandmarkContent con=new LandmarkContent();
            con.setContent(landmarkDetai.getLandmarkContent().getContent());
            con.setId(landmarkDetai.getId());
            landmarkContentMapper.insert(con);
        }
    }

    @Override
    public LtCatalogDetail getIdByCatalogBigId(Long cbgId) {
        return ltCatalogDetailMapper.getIdByCatalogBigId(cbgId);
    }

    @Override
    public List<LandmarkDetai> getlDetailByicdId(Long icdId) {
        return landmarkDetaiMapper.selectDetailByicdId(icdId);
    }
}
